package com.xywl.escort.view.home;

import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.RedPacketReq;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class NewWelfarePresenter implements NewWelfareContract.Presenter {
    private NewWelfareContract.View mView;
    private ApiServer mApiServer;

    @Inject
    NewWelfarePresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(NewWelfareContract.View view) {
        mView = view;
    }

    @Override
    public void processRedPacket(int bizCode) {
        RedPacketReq req = new RedPacketReq();
        req.bizCode = bizCode;
        mApiServer.processRedPacket(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.processRedPacketFailure("请求失败，请稍后重试");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.processRedPacketSuccess(resp);
                            } else {
                                mView.processRedPacketFailure(resp.msg);
                            }
                        } else {
                            mView.processRedPacketFailure("请求失败，请稍后重试");
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("HomePresenter").e("unSubscribe");
        mView = null;
    }
}
